package net.sx.minemodule.mvp.presenters;


import android.databinding.ObservableField;
import android.util.Log;
import android.view.View;

import com.homegoods.login.KGetVerHelper;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.mvp.bean.UpdatePwdParams;

/**
 * 修改登录密码
 */

public class MineLoginPasswordPresenter extends BasePresenter {
    private static final String TAG = "LoginPasswordPresentTag";
    private String mobile;

    /**
     * 验证码
     */
    public final ObservableField<String> strCode = new ObservableField<>();
    /**
     * 密码
     */
    public final ObservableField<String> strNewPwd = new ObservableField<>();
    private KGetVerHelper kGetVerHelper;

    public MineLoginPasswordPresenter(KGetVerHelper kGetVerHelper) {
        this.kGetVerHelper = kGetVerHelper;
    }

    /**
     * 修改密码提交按钮
     * @param view
     */
    public void submit(View view) {
        if (checkData(view)) {
            //modify shibo.zheng start
            String pwd = strNewPwd.get();
            String authCode = strCode.get();
            UpdatePwdParams params = new UpdatePwdParams(); //初始化修改密码参数bean
            params.setAuthCode(authCode);
            params.setTelephone(mobile);
            params.setPassword(MyUtils.MD5(pwd, 2));
            Log.d(TAG, "submit: token == " + HomeGoodsApp.getToken());
            MineApiUtils.modifyUserPhoneAndPwd(params, s -> {
                ToastUtil.showToast("修改成功");
            }, throwable -> {
                Log.d(TAG, "submit: throwable == " + throwable.toString());
            });
            //modify shibo.zheng end
        }
    }

    /**
     * 检查登录所需数据的合法性
     * @return
     */
    private boolean checkData(View view) {
        String code = strCode.get();
        String pwd = strNewPwd.get();
        //验证码不能为空
        if (StringUtil.isEmpty(code)) {
            ToastUtil.showSnackbar(view, "验证码不能为空");
            return false;
        }
        //验证码不正确
        if (StringUtil.isEmpty(mobile)) {
            ToastUtil.showSnackbar(view, "验证码不正确");
            return false;
        }
        if (!StringUtil.checkPassword(pwd)) {
            ToastUtil.showSnackbar(view, "请输入6-16位数字，字母");
            return false;
        }
        return true;
    }

    /**
     * 获取短信验证码
     */
    public void getVerCode(String phone) {
        mobile = phone;
        MineApiUtils.getCaptchaForForgetPassWord(phone, jsonElement -> {
            kGetVerHelper.sendSuc();

        }, throwable -> {
        });
    }
}
